package cqcvc.edu.cn.servlet;

import cqcvc.edu.cn.dao.MemberDao;
import cqcvc.edu.cn.entity.MemberInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginActionServlet", urlPatterns = "/loginAction")
public class LoginActionServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf8");
        response.setCharacterEncoding("utf8");
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        //访问数据库进行登录验证
        MemberDao md = new MemberDao();
        Boolean loginResult = md.Login(userName, password);
        if (loginResult) {//用户名密码验证正确
            try {
                MemberInfo mi = md.findByName(userName);
                request.getSession().setAttribute("memberInfo", mi);
                request.getRequestDispatcher("/pages/index/index.jsp").forward(request, response);
            }catch (Exception ex)
            {
            }
        } else {//用户名密码验证错误
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
